#!/usr/bin/env ruby
require 'erb'

require 'session'

TMPL = <<EOF
class <%= class_name.snake_case.camel_case %> < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end
EOF

if ARGV.size == 0
  puts 'usage new_migration MyNewMigration or my_new_migration'
  exit -1
end

class_name = ARGV[0]
highest_migration = Dir[Dir.pwd+'/db/migrations/*.rb'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max
filename = format("%03d_%s", (highest_migration.to_i+1), class_name.snake_case)

File.open(Dir.pwd+"/db/migrations/#{filename}.rb", 'w+') do |file|
  file.write ERB.new(TMPL).result(binding)
  puts "created #{filename}.rb"
end
